package com.ruoyi.rsikManage.controller;

import java.util.ArrayList;
import java.util.List;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.rsikManage.domain.TVideoStandPlace;
import com.ruoyi.rsikManage.domain.PlaceCategory;
import com.ruoyi.rsikManage.service.ITVideoStandPlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 视频场所标准字典Controller
 *
 * @author ruoyi
 * @date 2023-08-08
 */
@RestController
@RequestMapping("/zdzh/videoPlace")
@SuppressWarnings("all")
public class TVideoStandPlaceController extends BaseController
{
    @Autowired
    private ITVideoStandPlaceService tVideoStandPlaceService;

    @Autowired
    private TokenService tokenService;

    /**
     *
     * 查询场所类别字典
     * @param dicType t_video_parent_level或者t_video_child_level
     * @param parentDicVal
     * @return
     */
    @GetMapping("place-sort-dic")
    public AjaxResult selectPlaceCategoryDicList(String dicType, @RequestParam(value = "parentDicVal", required = false) String standardFileDicVal) {
        List<PlaceCategory> data = tVideoStandPlaceService.selectPlaceCategoryDicList(dicType, standardFileDicVal);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<PlaceCategory>();
        }
        return AjaxResult.success(data);
    }
    /**
     * 查询某标准文件的场所类别及对应的场所
     */
    @GetMapping("category-list-with-place")
    public AjaxResult categoryListWithPlace() {
        String provinceFileDicVal = "0";
        List<PlaceCategory> categoryList =  tVideoStandPlaceService.selectCategoryListWithPlace(provinceFileDicVal);
        return AjaxResult.success(categoryList);
    }
    /**
     * 查询视频场所标准字典列表
     */
    //@PreAuthorize("@ss.hasPermi('zdzh:videoPlace:list')")
    @GetMapping("/list")
    public TableDataInfo list(TVideoStandPlace tVideoStandPlace)
    {
        startPage();
        List<TVideoStandPlace> list = tVideoStandPlaceService.selectTVideoStandPlaceList(tVideoStandPlace);
        return getDataTable(list);
    }

    /**
     * 查询场所的同时携带该场所的监控点信息
     * @param tVideoStandPlace
     * @return
     */

    @GetMapping("/list-with-monitors/v2")
    public TableDataInfo placelistWithMonitors(TVideoStandPlace place)
    {
        startPage();
        List<TVideoStandPlace> list = tVideoStandPlaceService.selectPlaceListWithMonitors(place);
        return getDataTable(list);
    }

    /**
     * 查询场所的同时携带该场所的监控点信息
     * @param tVideoStandPlace
     * @return
     */

    @GetMapping("/list-with-monitors")
    public TableDataInfo placelistWithMonitorsForUnitCodes(TVideoStandPlace place, HttpServletRequest httpServletRequest)
    {
        startPage();
        Long unitCode = place.getUnitCode();
        if (unitCode == null){
            unitCode = tokenService.getLoginUserUnitCode(httpServletRequest);
            place.setUnitCode(unitCode);
        }
        List<TVideoStandPlace> list = tVideoStandPlaceService.selectPlaceListWithMonitorsForUnitCodes(place);
        return getDataTable(list);
    }

    /**
     * 给某个场所下新增一个监控点
     * @param placeParentLevelDicVal 场所父类别的字典值
     * @param placeId 场所id
     * @param monitorId 监控点id
     * @return
     */
    @PostMapping("add-monitor-rel-place/v2")
    public AjaxResult addMonitorInThePlace(String placeCagegoryDicVal, String placeName, String monitorId, boolean confirmDelFlag) {
        boolean isHasMonitorRelPlace = tVideoStandPlaceService.checkHasMonitorRelPlace(placeCagegoryDicVal, monitorId);
        if (isHasMonitorRelPlace && !confirmDelFlag) {
            return new AjaxResult(202, "该监控点现在部署在其他的场所，是否移动？");
        }
        tVideoStandPlaceService.delMonitorRelAllPlace(monitorId);
        tVideoStandPlaceService.addMonitorRelPlace(placeName, monitorId);
        return success("监控点添加成功！");
    }

    /**
     * 给某个场所下新增一个监控点
     * @param placeParentLevelDicVal 场所父类别的字典值
     * @param placeId 场所id
     * @param monitorId 监控点id
     * @return
     */
    @PostMapping("add-monitor-rel-place")
    public AjaxResult addMonitorInThePlaceForUnitCodes(String placeCagegoryDicVal,
                                                       String placeName,
                                                       String monitorId,
                                                       boolean confirmDelFlag,
                                                       Long unitCode) {
        boolean isHasMonitorRelPlace = tVideoStandPlaceService.checkHasMonitorRelPlaceForUnitCodes(placeCagegoryDicVal, monitorId,unitCode);
        if (isHasMonitorRelPlace && !confirmDelFlag) {
            return new AjaxResult(202, "该监控点现在部署在其他的场所，是否移动？");
        }
        tVideoStandPlaceService.delMonitorRelAllPlaceForUnitCodes(monitorId,unitCode);
        tVideoStandPlaceService.addMonitorRelPlaceForUnitCodes(placeName, monitorId,unitCode);
        return success("监控点添加成功！");
    }

    /**
     * 删除某个场所和监控点的关联
     *
     * @param placeParentLevelDicVal 场所父类别的字典值
     * @param placeId 场所id
     * @param monitorId 监控点id
     * @return
     */
    @DeleteMapping("del-monitor-rel-place/v2")
    public AjaxResult delMonitorRelPlace(String placeName, String monitorId) {
        tVideoStandPlaceService.delMonitorRelPlace(placeName, monitorId);
        return success();
    }

    /**
     * 删除某个场所和监控点的关联
     *
     * @param placeParentLevelDicVal 场所父类别的字典值
     * @param placeId 场所id
     * @param monitorId 监控点id
     * @return
     */
    @DeleteMapping("del-monitor-rel-place")
    public AjaxResult delMonitorRelPlaceForUnitCodes(String placeName, String monitorId,Long unitCode) {

        tVideoStandPlaceService.delMonitorRelPlace(placeName, monitorId);
        return success();
    }

}
